
public class Player {
	String name;
	int maxLife;// 最大生命
	int life;
	int level;
	int exp;
	int attack;
	int defense;
	int aglie;
	int critDamage;
	int critRate;
	boolean isDead;

	public Player(String name) {
		this.isDead = false;
		this.name = name;
		this.maxLife = 100;
		this.life = this.maxLife;
		this.level = 1;
		this.exp = 0;
		this.attack = 25;
		this.defense = 10;
		this.aglie = 20;
		this.critDamage = 200;
		this.critRate = 15;
		System.out.println(this.name + "在暗黑大陆诞生了！僵尸准备受死吧!");
	}

	public void show() {
		System.out.println("------------------------------------------------------------------------------------");
		System.out.println("姓名:" + this.name);
		System.out
				.println("\t最大生命:" + this.maxLife + "\t生命:" + this.life + "\t\t等级:" + this.level + "\t经验值:" + this.exp);
		System.out.println("\t攻击力:" + this.attack + "\t防御力:" + this.defense + "\t敏捷:" + this.aglie + "\t暴击率:"
				+ this.critRate + "\t暴击伤害:" + this.critDamage);
		System.out.println("------------------------------------------------------------------------------------");
	}

	public void fight(Enemy e) {
		while (true) {
			// 判断自己或者敌人是否死亡
			if (this.isDead || e.isDead) {
				break;
			}
			// 口号
			System.out.println(this.name + "举起大剑劈向[" + e.name + "]," + this.name + ":受我一刀");
			// 获取伤害
			int damage = attack - e.defense;
			// 判断伤害
			if (damage <= 0) {
				damage = 1;
			}
			// 暴击
			if (Tools.checkRate(critRate)) {
				// 产生暴击
				System.out.println(this.name + "哇哈哈哈哈！天神赐予我力量！[" + e.name + "]受死吧!");
				double tc = (double) critDamage / 100.0;
				double td = tc * damage;
				damage += Math.ceil(td);
			}
			// 让敌人受伤
			e.injured(this, damage);
		}
		// 完成了一轮攻击
		if (e.isDead) {
			this.exp += e.getExp;
			int nextLevel = this.getUpgradeExp(100, 20, level);
			if (this.exp >= nextLevel) {
				upgrade();
			}
		}
	}

	public void upgrade() {
		System.out.println(this.name + ":哇哈哈哈哈哈！天降神兵，力量在我身边围绕!");
		this.level += 1;
		this.maxLife += 20;
		this.life = this.maxLife;
		this.attack += 5;
		this.defense += 3;
		this.aglie += 3;
		this.critDamage += 50;
		this.critRate += 3;
		this.show();
	}

	public int getUpgradeExp(int base, int rate, int level) {
		double drate = (double) rate / 100.0;
		int uexp = (int) (base * drate);
		int sum = base;
		for (int i = 1; i < level; i++) {
			sum += (base + i * uexp);
		}
		return sum;
	}

	public boolean hide() {
		boolean b = Tools.checkRate(aglie);
		return b;
	}

	public void injured(int damage) {
		// 口号
		System.out.println(this.name + ":哇!居然敢反击![" + damage + "]");
		// 闪避
		boolean isHide = hide();
		if (isHide) {
			System.out.println(this.name + ":嘻嘻嘻嘻嘻!小样！打不到我!");
			return;
		}
		// 扣血
		this.life -= damage;
		// 判断死亡
		if (this.life <= 0) {
			this.isDead = true;
			System.out.println(this.name + ":天妒英才!时局负我啊！！！！");
			System.out.println("大侠！生不逢时，是否重新来过");
		}
	}
}
